package com.aisile.sellergoods.service.impl;


import com.aisile.entity.PageResult;
import com.aisile.mapper.TbBrandMapper;
import com.aisile.pojo.TbBrand;

import com.aisile.pojo.TbBrandExample;
import com.aisile.sellergoos.service.BrandService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    TbBrandMapper tbBrandMapper;

    public List<TbBrand> findAll() {
        return tbBrandMapper.selectByExample(null);
    }


    public PageResult findPage(int page, int rows, TbBrand brand) {
        PageHelper.startPage(page,rows);

        String name=brand.getName();
        String firstChar=brand.getFirstChar();
        TbBrandExample tbBrandExample=new TbBrandExample();
        if(!StringUtils.isEmpty(name)){
            tbBrandExample.createCriteria().andNameLike("%"+name+"%");
        }
        if(!StringUtils.isEmpty(firstChar)){
            tbBrandExample.createCriteria().andFirstCharLike("%"+firstChar+"%");
        }

        Page<TbBrand> pageResult=(Page<TbBrand>) tbBrandMapper.selectByExample(tbBrandExample);
        return new PageResult(pageResult.getTotal(),pageResult.getResult());
    }


    public void save(TbBrand brand) {

        tbBrandMapper.insert(brand);
    }

    @Override
    public void update(TbBrand brand) {
        tbBrandMapper.updateByPrimaryKey(brand);
    }

    @Override
    public TbBrand findOne(long id) {
        return tbBrandMapper.selectByPrimaryKey(id);
    }

    @Override
    public void dele(Long[] ids) {
        if(ids!=null){
            for (Long id:ids){
                tbBrandMapper.deleteByPrimaryKey(id);
            }
        }
    }
}
